(function(window){
	class Bullet{
		bulletInfo={
			
			id:'',
			x_direction:1,//子弹向x轴发射的方向 1是向左，-1则是向右.根据雷霆战机的位置 设置子弹发射的方向
			y_direction:1,//子弹向y轴发射的方法 1是向上 -1则是向下.根据雷霆战机的位置 设置子弹发射的方向
			trajectoryAngle:'',//子弹轨迹的角度 通过获取初始化鼠标的y轴距离与x轴距离，就是二者之间的比例
			location:{ //当前子弹的位置
			    initx:'',//子弹的初始x位置
				inity:'',//子弹的初始y位置
				currentx:'',//子弹当前的x位置
				currenty:''//子弹当前的y位置
			},
			bulletImg:'url(./img/篮球.png)',
			color:'red',
			width:'5',
			height:'5',
			step:'5',//子弹每一帧走多远，相当于速度吧
			
		}
		windowInfo={
			width:'',
			height:''
		}
		mouseInfo={
			
		}
		bulletEl=null //子弹元素
		ifMove=null //子弹是否移动
		constructor({thunderFighterInfo,mouseInfo}){
			this.thunderFighterInfo=thunderFighterInfo //初始化当前雷霆战机的位置
			this.mouseInfo=mouseInfo	
			this.initBulletInfo()
			
		}	
		initBulletInfo(){//初始化子弹
			
			//初始化子弹数据
			this.bulletInfo.id=this.elId()
			this.bulletInfo.location.initx=this.thunderFighterInfo.offsetLeft+(this.thunderFighterInfo.width/2)
			this.bulletInfo.location.inity=this.thunderFighterInfo.offsetTop+(this.thunderFighterInfo.height/2)
			const x=(this.thunderFighterInfo.offsetLeft+(this.thunderFighterInfo.width/2)) - (this.mouseInfo.clientX)
			const y=(this.thunderFighterInfo.offsetTop+(this.thunderFighterInfo.height/2)) - (this.mouseInfo.clientY)
			if(x<0){
				this.bulletInfo.x_direction=-1
			}
			if(y<0){
				this.bulletInfo.y_direction=-1
			}
			if(x/y<0){
			
				this.bulletInfo.trajectoryAngle=x/y*(-1)
			}else{
				this.bulletInfo.trajectoryAngle=x/y
			}
				
			
			
			this.initBulletEl()
		}
		
		initBulletEl(){//创建子弹元素单位并设置基本信息
		
	
			this.bulletEl=document.createElement("div")
			this.bulletEl.style.position='absolute'
			this.bulletEl.setAttribute('id',this.bulletInfo.id)

			
			window.document.querySelector('body').append(this.bulletEl)//将子弹添加到页面
			
			this.bulletEl.style.backgroundImage =this.bulletInfo.bulletImg
			this.bulletEl.style.backgroundSize="5px 5px";
			this.bulletEl.style.borderRadius=" 50% 50%";
			// this.bulletEl.style.backgroundColor=this.bulletInfo.color
			this.bulletEl.style.width=this.bulletInfo.width+'px'
			this.bulletEl.style.height=this.bulletInfo.height+'px'
			
			this.bulletEl.style.left=this.bulletInfo.location.initx+'px'
			this.bulletEl.style.top=this.bulletInfo.location.inity+'px'
			
				requestAnimationFrame(()=>{
					this.updateBulletInfo()
				})
			
		
		}
		
		updateBulletInfo(){//更新子弹数据 例如位置颜色等
			
			if(this.bulletInfo.location.currentx===''){
			
				this.bulletInfo.location.currentx=this.bulletInfo.location.initx-(this.bulletInfo.step*this.bulletInfo.x_direction)
				this.bulletInfo.location.currenty=this.bulletInfo.location.inity-(this.bulletInfo.step*this.bulletInfo.y_direction/this.bulletInfo.trajectoryAngle)
		
			}else{
			
				this.bulletInfo.location.currentx=this.bulletInfo.location.currentx-(this.bulletInfo.step*this.bulletInfo.x_direction)
				this.bulletInfo.location.currenty=this.bulletInfo.location.currenty-(this.bulletInfo.step*this.bulletInfo.y_direction/this.bulletInfo.trajectoryAngle)
			}

			
			this.updateBulletEl()
		}
		
		updateBulletEl(){//更新子弹元素
			this.windowInfo.width=document.documentElement.clientWidth
			this.windowInfo.height=document.documentElement.clientHeight
			
			this.bulletEl.style.backgroundColor=this.bulletInfo.color
			this.bulletEl.style.width=this.bulletInfo.width+'px'
			this.bulletEl.style.height=this.bulletInfo.height+'px'
			
			this.bulletEl.style.left=this.bulletInfo.location.currentx+'px'
			this.bulletEl.style.top=this.bulletInfo.location.currenty+'px'
				
			if(this.bulletInfo.location.currentx<0||this.bulletInfo.location.currentx>this.windowInfo.width || this.bulletInfo.location.currenty<0 || this.bulletInfo.location.currenty>this.windowInfo.height){
				this.moveBulletEl()
				
			}else{
				var isAlive=true //判断子弹是否还活着 就是默认没有射到怪物
				
				window.monsterList.forEach((monster,index)=>{
					
					if(
					(monster.monsterEl.offsetTop+monster.monsterEl.offsetHeight)>this.bulletEl.offsetTop&&this.bulletEl.offsetTop>monster.monsterEl.offsetTop 
					&&
					(monster.monsterEl.offsetLeft+monster.monsterEl.offsetWidth)>this.bulletEl.offsetLeft&&this.bulletEl.offsetLeft>monster.monsterEl.offsetLeft
					){
						
						isAlive=false
						document.querySelector('body').removeChild(monster.monsterEl)
						
						window.monsterList.splice(index,1)
						
						
					}
				})
				
				
					
				if(isAlive){
					requestAnimationFrame(()=>{
								
						this.updateBulletInfo()
					})
				}else{
					this.moveBulletEl()
					window.monsterList.push(new Monster())
				}
				
				
				
			}
			
				
				
				
			
		}
		
		//移除子弹元素
		moveBulletEl(){
			// console.log(this.bulletInfo.id)
			document.querySelector('body').removeChild(this.bulletEl)
		}
		
		
		//生成唯一元素id
		 elId() {
		    var s = [];
		    var hexDigits = "0123456789abcdef";
		    for (var i = 0; i < 36; i++) {
		        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
		    }
		    s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
		    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
		    s[8] = s[13] = s[18] = s[23] = "-";
		
		    var uuid = s.join("");
			uuid='bullet'+uuid
		    return uuid;
		}
		
	}
	
	window.Bullet=Bullet
})(window);